﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Animation;
using FarseerPhysics.Dynamics.Joints;
using FarseerPhysics;
using FarseerPhysics.Dynamics;
using FarseerPhysics.Dynamics.Contacts;
using Enemies;
using FarseerPhysics.Controllers;
using MapLibrary.Enemies;

namespace MapLibrary.Players
{
    public class Bullet : Sprite
    {
        /// <summary>
        /// the amount of linear damping to apply when a Bullet gets stuck to the player
        /// </summary>
        const float DAMPING = 3.0f;

        public Sprite Owner { get; set; }

        /// <summary>
        /// the joint to be used when the spit collides with the player
        /// </summary>
        private WeldJoint joint;

        public Bullet()
        {
            MAX_VELOCITY_X = ConvertUnits.ToSimUnits(1000);
            MAX_VELOCITY_Y = ConvertUnits.ToSimUnits(1000);
            this.Controller.MaxLinearVelocityX = MAX_VELOCITY_X;
            this.Controller.MaxLinearVelocityY = MAX_VELOCITY_Y;

        }

        public override void Update()
        {
            base.Update();

            if (World != null)
            {
                if (!World.Viewport.IsWorldPointInViewport(Position))
                    World.ProjectileManager.RemoveBullet(this);
            }
        }

        /// <summary>
        /// if the spit collides with the player, create a joint between them and increase the players linear damping
        /// if the spit collides with anything else, remove it from the world and recycle it
        /// </summary>
        /// <param name="f1"></param>
        /// <param name="f2"></param>
        /// <param name="contact"></param>
        /// <returns></returns>
        public override bool MyOnCollision(Fixture f1, Fixture f2, Contact contact)
        {
            //Player s2 = f2.Body.UserData as Player;
            //TuberculosisSpit s3 = f2.Body.UserData as TuberculosisSpit;

            //if (s2 == null)
            //{
            //    Owner.World.CurrentLevel.RemoveSprite(this);
            //    return true;
            //}

            //if (s3 != null)
            //    return false;
            //if((f2.Body.UserData as Player) == null)
            //    World.ProjectileManager.RemoveBullet(this);

            LevelCollisionRegion lcr = f2.Body.UserData as LevelCollisionRegion;
            if (lcr != null)
            {
                World.CurrentLevel.AddBodyToRemove(this);
                return false;
            }

            Enemy e = f2.Body.UserData as Enemy;
            if (e != null)
            {
                e.DealDamage(25);
                World.CurrentLevel.AddBodyToRemove(this);
                return true;
            }

            HookableObject ho = f2.Body.UserData as HookableObject;
            if (ho != null)
            {
                World.CurrentLevel.AddBodyToRemove(this);
                return true;
            }

            return false;
        }

        /// <summary>
        /// Gets called when the joint between a Bullet and the player breaks
        /// </summary>
        /// <param name="j"></param>
        /// <param name="f"></param>
        public void OnJointBreak(Joint j, float f)
        {
            
        }
    }
}
